<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: AddUserAccount.php
 * Date: 2017/2/12
 * Time: 1:08
 */

namespace app\api\model\page\user;

use app\api\model\dao\UserAccount;
use app\api\model\dao\UserCurrency;
use app\library\Error;
use app\library\PageBase;
use think\Db;


class AddUserAccount extends PageBase
{
    private $objUser;

    public function __construct()
    {
        parent::__construct();
        $this->objUser = new UserAccount();
    }

    public function excute($param)
    {
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }
        if (isset($param['user_id'])) {
            //穿入userid就是更新数据
            $userInfo['id'] = $param['user_id'];
            //var_dump($param);
            $ret = $this->objUser->updateUserAccount($param);
            if (false === $ret) {
                return $this->getRes(Error::ERR_SYS);
            }
        } else {
            $userInfo['user_name'] = $param['user_name'];
            if (isset($param['nick_name'])) {
                $userInfo['nick_name'] = $param['nick_name'];
            }
            if (isset($param['phone'])) {
                $userInfo['phone'] = $param['phone'];
            }
            if (isset($param['flag'])) {
                $userInfo['flag'] = $param['flag'];
            }
            if (isset($param['age'])) {
                $userInfo['age'] = $param['age'];
            }
            if (isset($param['sex'])) {
                $userInfo['sex'] = $param['sex'];
            }
            if (isset($param['avatar'])) {
                $userInfo['avatar'] = $param['avatar'];
            }
            if (isset($param['reg_time'])) {
                $userInfo['reg_time'] = time();
            }
            if (isset($param['reg_ip'])) {
                $userInfo['reg_ip'] = '127.0.0.1';
            }
            //extend_info
            $extend_info = [];
            if (isset($param['constellation'])) {
                $extend_info['constellation'] = $param['constellation'];
            }
            if (isset($param['dating_intention'])) {
                $extend_info['dating_intention'] = $param['dating_intention'];
            }
            if (isset($param['marriage_status'])) {
                $extend_info['marriage_status'] = $param['marriage_status'];
            }
            if (isset($param['tag'])) {
                $extend_info['tag'] = $param['tag'];
            }
            if (isset($param['location'])) {
                $extend_info['location'] = $param['location'];
            }
            if (isset($param['introduction'])) {
                $extend_info['introduction'] = $param['introduction'];
            }
            if (isset($param['sexual_orientation'])) {
                $extend_info['sexual_orientation'] = $param['sexual_orientation'];
            }
            if (isset($param['home'])) {
                $extend_info['home'] = $param['home'];
            }
            $userInfo['extend_info'] = json_encode($extend_info);

            Db::startTrans();
            try {
                $ret = $this->objUser->addUserAccount($userInfo);
                //初始化漫豆漫币
                $objCurrency = new UserCurrency();
                $objCurrency->initCurrency($ret);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                \think\Log::error('init User_Currency failed! ' . json_encode($param));
                return $this->getRes(Error::ERR_SYS);

            }
            $this->data = [$ret];
        }
        return $this->getRes();
    }


    public function checkParam($param)
    {
        $rule = [
            'user_name' => 'require',
        ];
        $msg = [
            'user_name.require' => 'user_name必须',
        ];
        $validate = new \think\Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function zeroCount($user_id, $type)
    {
        return [
            'user_id' => (int)$user_id,
            'currency_type' => (int)$type,
            'amount' => 0
        ];
    }
}
